﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using LosConn.BLL;

namespace LosConn.Web.Handler
{
    /// <summary>
    /// RetailHandler 的摘要说明
    /// </summary>
    public class RetailHandler : IHttpHandler, IReadOnlySessionState
    {
        private BLL.Los_Retail bll = new BLL.Los_Retail();
        public void ProcessRequest(HttpContext context)
        {
            String strResult = String.Empty;
            switch (context.Request["fun"])
            {

                case "f_0001": //新增门市
                    strResult = AddRetail(context);
                    break;
                case "f_0002": //修改门市
                    strResult = UpdateRetail(context);
                    break;
                //case "f_0003": //确认
                //    strResult = UpdateGuestConfirm(context);
                //    break;
                default:
                    break;
            }

            context.Response.Write(strResult);
            context.Response.End();
        }

        #region 私有方法
        /// <summary>
        /// 新增门市
        /// </summary>
        private string AddRetail(HttpContext context)
        {
            Model.Los_Retail model = JsonConvert.DeserializeObject<Model.Los_Retail>(context.Request["data"]);
            model.Retail_EMail = "";
            //model.Retail_RackPhone = "";
            model.Retail_UserID = Convert.ToInt32(context.Session["UserID"]);
            model.Retail_UserName = context.Session["UserName"].ToString();
            Int32 retMsg = bll.Add(model);
            if (retMsg > 0)
            {
                if (!model.Retail_Money.Equals(Convert.ToDecimal(0)))
                {
                    BLL.Los_Pay bllPay = new Los_Pay();
                    Model.Los_Pay modelPay = new Model.Los_Pay();
                    modelPay.Pay_Money = model.Retail_Money;
                    modelPay.Pay_PayRetailID = retMsg;
                    modelPay.Pay_Remark = "门市开户预存款";
                    modelPay.Pay_UserID = Convert.ToInt32(context.Session["UserID"]);
                    modelPay.Pay_UserName = context.Session["UserName"].ToString();
                    modelPay.Pay_AddDate = DateTime.Now;
                    bllPay.AddPay(modelPay);
                }
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";
            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 修改门市
        /// </summary>
        private string UpdateRetail(HttpContext context)
        {
            Model.Los_Retail modelRetail = bll.GetModel(Convert.ToInt32(context.Request.Form["id"]));
            Model.Los_Retail model = JsonConvert.DeserializeObject<Model.Los_Retail>(context.Request["data"]);
            model.Retail_ID = modelRetail.Retail_ID;
            model.Retail_EMail = modelRetail.Retail_EMail;
            //model.Retail_RackPhone = modelRetail.Retail_RackPhone;
            model.Retail_UserID = modelRetail.Retail_UserID;
            model.Retail_UserName = modelRetail.Retail_UserName;
            model.Retail_Name = modelRetail.Retail_Name;
            model.Retail_Money = modelRetail.Retail_Money;
            //model.Retail_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.Update(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}